﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using Emerson.Storage.Service.Interface;
using Emerson.Storage.Entities;

namespace WorkstationDashboard
{
    public static class SessionKeys
    {
        public const string WorkstationRoot = "WorkstationRoot";
    }

    public static class Utility
    {
        public static TreeNode BuildTreeFromWorkstationRoot(WorkstationRoot wr)
        {
            TreeNode treeRoot = new TreeNode(wr.Name);
            treeRoot.ImageUrl = @"images\root.png";
            treeRoot.Value = "0";
            foreach (var curGroup in wr.Groups)
            {
                TreeNode treeGroup = new TreeNode(curGroup.Name);
                treeGroup.ImageUrl = @"images\groupsmall.png";
                treeGroup.Value = curGroup.GroupId.ToString();
                treeRoot.ChildNodes.Add(treeGroup);
                foreach (var curWorkstation in curGroup.Workstation)
                {
                    TreeNode treeWorkstation = new TreeNode(curWorkstation.Name);
                    if (curWorkstation.IsConnected)
                        treeWorkstation.ImageUrl = @"images\workstationOkSmall.png";
                    else
                        treeWorkstation.ImageUrl = @"images\workstationGraySmall.png";
                    treeWorkstation.Value = curWorkstation.WorkstationId.ToString();
                    treeGroup.ChildNodes.Add(treeWorkstation);
                    foreach (var curBattery in curWorkstation.Battery)
                    {
                        TreeNode treeBattery = new TreeNode(curBattery.BatteryType + curBattery.BatteryId.ToString());
                        if (curBattery.IsConnected)
                            treeBattery.ImageUrl = @"images\batteryOksmall.png";
                        else
                            treeBattery.ImageUrl = @"images\batteryDisconnectedSamll.png";
                        treeBattery.Value = curBattery.BatteryId.ToString();
                        treeWorkstation.ChildNodes.Add(treeBattery);
                    }
                }
            }

            treeRoot.ExpandAll();
            return treeRoot;
        }

        internal static void SelectNode(TreeView treeView, NodeType nodeType, int id)
        {
            TreeNode root = treeView.Nodes[0];
            if (nodeType == NodeType.Root)
            {
                root.Select();
                return;
            }
            int curId;
            foreach (TreeNode group in root.ChildNodes)
            {
                curId = int.Parse(group.Value);
                if (nodeType == NodeType.Group && curId == id)
                {
                    group.Select();
                    return;
                }
                foreach (TreeNode workstation in group.ChildNodes)
                {
                    curId = int.Parse(workstation.Value);
                    if (nodeType == NodeType.Workstation && curId == id)
                    {
                        workstation.Select();
                        return;
                    }
                    foreach (TreeNode device in workstation.ChildNodes)
                    {
                        curId = int.Parse(device.Value);
                        if (nodeType == NodeType.Device && curId == id)
                        {
                            device.Select();
                            return;
                        }
                    }

                }
            }
        }
    }

    public static class ServiceHelper
    {
        public static TService GetService<TService>()
        {
            Type serviceType = typeof(TService);
            string typeName = serviceType.Name;

            BasicHttpBinding myBinding = new BasicHttpBinding();
            string workstationAddress = "http://localhost:8732/EmersonStorageService/Services/" + typeName;

            EndpointAddress wsEndpoint = new EndpointAddress(workstationAddress);
            var wsFactory = new ChannelFactory<TService>(myBinding, wsEndpoint);

            TService wsService = wsFactory.CreateChannel();
            return wsService;
        }
    }

    enum NodeType
    {
        Root,
        Group,
        Workstation,
        Device
    }
}